﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace syc.Store
{
    public partial class 产品入出库台账 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Notice = "·提 示：为了保证系统运行性能，<br>每次最多能查询2000条(约100页)数据。";
            }
            else
            {
                this.SqlDataSource3.SelectCommand = (string)this.ViewState["SelectCommand"];
            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        private string getSql()
        {
            string sql =
@"select top 2000 原料供货单位,
case
when 生产线=101 then '一分厂'
when 生产线=201 then '二分厂1号线'
when 生产线=202 then '二分厂2号线'
when 生产线=501 then '五分厂1号线'
when 生产线=502 then '五分厂2号线'
else '未知分厂'
end
as 生产线
,入库序号,入库日期,炉号,支数,捆号
,left(right('00000'+cast(捆号 as varchar),9),4)+'-'+right(cast(捆号 as varchar),4) as 捆编号
,重量,长度,生产日期,库区,出库日期,运输方式,出库序号,[ID] from 产品入出库台账 where 生产线="
                + this.FenChang.Text + " and 产品类别ID=" + this.ChanPin.Text;
            int gonghuo = 0;
            int dinghuo = 0;

            DateTime d1, d2, d3, d4;
            int.TryParse(this.GongHuo.Text, out gonghuo);
            int.TryParse(this.DingHuo.Text, out dinghuo);

            if (gonghuo != 0)
            {
                sql += " and 原料供货ID=" + gonghuo;

            }
            if (dinghuo != 0)
            {
                sql += " and 订货单位ID=" + dinghuo;

            }


            if (DateTime.TryParse(Date1.Text, out d1))
            {
                sql += " and 入库日期>='" + d1.ToShortDateString() + "'";
            }
            else if (Date1.Text != "")
            {
                this.Notice = "入库起始日期输入有误";
                return "";
            }

            if (DateTime.TryParse(Date2.Text, out d2))
            {
                sql += " and 入库日期<='" + d2.ToShortDateString() + "'";
            }
            else if (Date2.Text != "")
            {
                this.Notice = "入库截止日期输入有误";
                return "";
            }

            if (DateTime.TryParse(Date3.Text, out d3))
            {
                sql += " and 出库日期>='" + d3.ToShortDateString() + "'";
            }
            else if (Date3.Text != "")
            {
                this.Notice = "出库起始日期输入有误";
                return "";
            }
            if (DateTime.TryParse(Date4.Text, out d4))
            {
                sql += " and 出库日期<='" + d4.ToShortDateString() + "'";
            }
            else if (Date4.Text != "")
            {
                this.Notice = "出库截止日期输入有误";
                return "";
            }

            string kh;
            int t;
            kh = this.KunHao1.Text.Replace("-", "0");
            if (kh.Length == 9 || int.TryParse(kh, out t))
            {
                sql += " and 捆号>=" + kh ;
            }
            kh = this.KunHao2.Text.Replace("-", "0");
            if (kh.Length == 9 || int.TryParse(kh, out t))
            {
                sql += " and 捆号<=" + kh;
            }
            int xh;
            if (this.XuHao1.Text.Trim() != "")
            {
                if (int.TryParse(this.XuHao1.Text, out xh))
                {
                    sql += " and 入库序号>=" + xh;
                }
                else
                {
                    this.Notice = "“起始序号”录入有误";
                    return "";
                }
            }
            if (this.XuHao2.Text.Trim() != "")
            {
                if (int.TryParse(this.XuHao2.Text, out xh))
                {
                    sql += " and 入库序号<=" + xh;
                }
                else
                {
                    this.Notice = "“截止序号”录入有误";
                    return "";
                }
            }
            return sql+" order by 捆号";
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            this.SqlDataSource3.SelectCommand = getSql();

            //Response.Write(this.SqlDataSource3.SelectCommand);

            this.ViewState["SelectCommand"] = getSql();
        }
        protected void Button_Export_Click(object sender, EventArgs e)
        {
            int i;
            string sql = getSql();
            if (sql == "") return;
            string op = (string)this.Session["OperatorName"];
            if (op == null)
            {
                op = "";
            }
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=\"" + Server.UrlEncode("产品入出库台账.xls") + "\"");
            Response.Write(
@"<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns='http://www.w3.org/TR/REC-html40'>
<head>
<meta http-equiv=Content-Type content='text/html; charset=utf-8'>
<style>
<!--table
@page
{mso-header-data:;
mso-footer-data:;
margin:.59in .31in .59in .31in;
mso-header-margin:.51in;
mso-footer-margin:.51in;
mso-page-orientation:landscape;
mso-horizontal-page-align:center;}
tr{height:25px;}
td{font-size:11pt;line-height:11pt;text-align:center;mso-number-format:'@';border:0.5pt solid #000000;}
td.nb{border:none;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>产品出库清单</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:Print>
<x:ValidPrinterInfo/>
<x:Scale>100</x:Scale>
<x:PaperSizeIndex>9</x:PaperSizeIndex>
<x:HorizontalResolution>200</x:HorizontalResolution>
<x:VerticalResolution>200</x:VerticalResolution>
</x:Print>
<x:ShowPageBreakZoom/>
<x:PageBreakZoom>100</x:PageBreakZoom>
<x:Selected/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>");
            if (this.JiOu.Text != "0")
            {
                
                int sr=1;
                if (this.JiOu.Text == "2")
                {
                    sr = 26;
                }


                Response.Write("\n\r<x:ActiveRow>" + (sr - 1) + "</x:ActiveRow>");
                Response.Write("\n\r<x:ActiveCol>0</x:ActiveCol>");

                Response.Write("\n\r<x:RangeSelection>");
                Response.Write("$A$" + sr + ":$M$" + (sr + 24));
                for (i = 1; i <= 60; i++)
                {
                    sr += 50;
                    Response.Write(",$A$" + sr + ":$M$" + (sr + 24));
                }
                Response.Write("</x:RangeSelection>");
            }
            Response.Write(
@"
</x:Pane>
</x:Panes>
</x:WorksheetOptions>
<x:PageBreaks>
<x:RowBreaks>");
            for (i = 1; i <= 120; i++)
            {
                Response.Write("<x:RowBreak><x:Row>" + (i * 25) + "</x:Row></x:RowBreak>");
            }
            Response.Write(
@"</x:RowBreaks>
</x:PageBreaks>
</x:ExcelWorksheet>
</x:ExcelWorkbook>
</xml><![endif]-->
</head><body><table border=0 cellpadding=0 cellspacing=0 style=text-align:center;border-collapse:collapse;font-size:11pt;>");

            string[] ss=this.ChanPin.SelectedItem.Text.Split(" ".ToCharArray());
            string head =
@"<tr><td class=nb colspan=13 style=height:40px;font-size:20pt;font-weight:bold;line-height:40px;>成品入出库登记台帐</td></tr>
<tr>
<td class=nb colspan=2 style=padding-left:20px;text-align:left;>　产品名称：" + ss[0] + @"</td>
<td class=nb colspan=2>钢级："+ss[1]+@"</td>
<td class=nb colspan=3 style=padding-left:8px;text-align:left;>规格：" + ss[2] + @"</td>
<td class=nb colspan=4 style=padding-left:8px;text-align:left;>订货单位：</td>
<td class=nb colspan=2>编码：SYC-ZJ-WL-11</td>
</tr>
<tr>
<td width=120 style=height:38px;>原料供货单位</td>
<td width=40>入库序号</td>
<td width=80>入库日期</td>
<td width=80>炉号</td>
<td width=40>支数</td>
<td width=90>捆编号</td>
<td width=70>捆重量(kg)</td>
<td width=70>捆长度(m)</td>
<td width=80>生产日期</td>
<td width=40>存放区域</td>
<td width=80>出库日期</td>
<td width=80>发运方式</td>
<td width=70>出库序号</td>
</tr>";
            string foot =
@"<tr>
<td class=nb colspan=3 style=padding-left:30px;text-align:left;>仓储主管：马玮</td>
<td class=nb colspan=7></td>
<td class=nb colspan=3 style=padding-left:30px;text-align:left;>保管员：" + op + "</td></tr>";
            Response.Write(head);
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = sql;
            Cmd.CommandType = CommandType.Text;

            SqlDataReader Reader = Cmd.ExecuteReader();
            int line = 0;
            int zhi = 0;//累计支数
            int kun = 0;//累计捆数
            decimal zhong = 0;//累计捆重
            decimal chang = 0;//累计捆长
            string xuhao0 = "";
            string xuhao = "";
            string dw1 = "",dw2="";
            while (Reader.Read())
            {
                if (!Convert.IsDBNull(Reader["入库序号"]))
                {
                    xuhao = ((int)Reader["入库序号"]).ToString();
                }
                if (xuhao != xuhao0 && kun > 0)
                {
                    Response.Write("<tr><td colspan=13>合计： " + zhi + "支　　　　" + kun + "捆　　　　" + (zhong / 1000) + "吨　　　　" + chang + "m</td></tr>");
                    line++;
                    zhi = 0;
                    kun = 0;
                    zhong = 0;
                    chang = 0;
                    dw1 = "";
                }
                if (line >= 20)
                {
                    Response.Write(foot);
                    if (line == 20)
                    {
                        Response.Write(
@"<tr><td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td>
<td class=nb></td></tr>");
                    }

                    Response.Write(head);
                    line = 0;
                    dw1 = "";
                }

                if (!Convert.IsDBNull(Reader["原料供货单位"]))
                {
                    dw2=(string)Reader["原料供货单位"];
                }

                xuhao0 = xuhao;
                zhi += (byte)Reader["支数"];
                kun++;
                zhong += (decimal)Reader["重量"];
                chang += (decimal)Reader["长度"];
                Response.Write("<tr><td>");
                if (dw2!=dw1)
                {
                    dw1 = dw2;
                    Response.Write(dw1);
                }
                Response.Write("</td><td>");
                if (!Convert.IsDBNull(Reader["入库序号"]))
                {
                    Response.Write(xuhao);
                }

                Response.Write("</td><td>" + ((DateTime)Reader["入库日期"]).ToString("yyyyMMdd") + "</td>"
                    + "<td>" + (string)Reader["炉号"] + "</td>"
                    + "<td>" + (byte)Reader["支数"] + "</td>"
                    + "<td>" + (string)Reader["捆编号"] + "</td>"
                    + "<td>" + (decimal)Reader["重量"] + "</td>"
                    + "<td>" + (decimal)Reader["长度"] + "</td>"
                    + "<td>" + ((DateTime)Reader["生产日期"]).ToString("yyyyMMdd") + "</td>"
                    + "<td>" + (byte)Reader["库区"] + "</td><td>"
                    );
                if (!Convert.IsDBNull(Reader["出库日期"]))
                {
                    Response.Write(((DateTime)Reader["出库日期"]).ToString("yyyyMMdd"));
                }
                Response.Write("</td><td>");
                if (!Convert.IsDBNull(Reader["运输方式"]))
                {
                    Response.Write((string)Reader["运输方式"]);
                }
                Response.Write("</td><td>");
                if (!Convert.IsDBNull(Reader["出库序号"]))
                {
                    Response.Write((int)Reader["出库序号"]);
                }
                Response.Write("</td></tr>");
                line++;
            }

            if (kun > 0)
            {
                Response.Write("<tr><td colspan=13>合计： " + zhi + "支　　　　" + kun + "捆　　　　" + (zhong / 1000) + "吨　　　　" + chang + "m</td></tr>");
                line++;
            }


            Reader.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Close();
            Conn.Dispose();

            if (line > 0)
            {
                for (i = line; i < 20; i++)
                {
                    Response.Write(
 @"<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>");
                }
                Response.Write(foot);
            }
            Response.Write("</table></body></html></body></html>");
            Response.End();

        }

        protected void Button_Advanced_Click(object sender, EventArgs e)
        {
            if (this.Panel2.Visible)
            {
                this.KunHao1.Text = "";
                this.KunHao2.Text = "";

                this.Date1.Text = "";
                this.Date2.Text = "";
                this.Date3.Text = "";
                this.Date4.Text = "";
                this.GongHuo.Text = "0";
                this.DingHuo.Text = "0";
                this.Panel2.Visible = false;
            }
            else
            {
                this.Panel2.Visible = true;
            }
        }



    }
}

